<?php
include_once "src/controller/dbmanagement/DBConnection.php";
include_once "src/rrsexception/DBConnectionException.php";
include_once "src/rrsexception/DBException.php";
include_once "src/model/Resource.php";
/**
 * It includes all the functions for the management of the data related to the
 * resource information.
 */
class ResourceDBManagement
{
    /**
     * Returns Resource Object with resource information in it if the resource
     * table contains the resource.
     */
     public static function findByResourceId($resourceId)
     {
         try
         {   
             $cnn = new DBConnection();
             $resource = new Resource();
             $query = "SELECT * FROM tResource WHERE tResource.Resource_id = '$resourceId'";
             $result = $cnn -> execute($query);
             $num = mysql_numrows($result);
             if ($num == 0) //resource not in database
             {
                 //print "resource ID $resourceID is not in the database\n";
                 $cnn -> disconnect();
                 return FALSE;
             }
             
             $ownerId = mysql_result($result, 0, "Owner_id");
             $resourceDescription = mysql_result($result, 0, "Description");
             $resourceLocation = mysql_result($result, 0, "Location");
             $resourceName = mysql_result($result, 0, "Name");
             $resourceGroup = mysql_result($result, 0, "Group_Name");
             $resourceStatus = mysql_result($result, 0, "Status_Value");
             $resource -> setAll($resourceName, $ownerId, $resourceDescription,
                                 $resourceLocation, $resourceGroup, $resourceStatus);
             $resource -> setResourceId($resourceId);
             $cnn->disconnect();
             
             return $resource;
         }
         catch (DBException $e)
         {
             throw $e;
         }
         catch (DBConnectionException $e)
         {
             throw $e;
         }
     }

     /**
      * Returns Resource Object with resource information in it if the resouce
      * table contains a resource with the resource id
      */
      public static function findByOwnerId($ownerId)
      {
          try
          {
              $cnn = new DBConnection();
              $resource = new Resource();
              $query = "SELECT * FROM tResource WHERE Owner_id = '$ownerId'";
              $result = $cnn -> execute($query);
              $num = mysql_numrows($result);
              if ($num == 0) //resource not in database
              {
                  $cnn->disconnect();
                  return FALSE;
              }
              $resourceId = mysql_result($result, 0, "Resource_id");
              $ownerId = mysql_result($result, 0, "Owner_id");
              $resourceDescription = mysql_result($result, 0, "Description");
              $resourceLocation = mysql_result($result, 0, "Location");
              $resourceName = mysql_result($result, 0, "Name");
              $resourceGroup = mysql_result($result, 0, "Group_Name");
              $resourceStatus = mysql_result($result, 0, "Status_Value");
              $resource->setAll($resourceName, $ownerId, $resourceDescription,
                                $resourceLocation, $resourceGroup, $resourceStatus);
              $resource -> setResourceId($resourceId);
              $cnn->disconnect();
              return $resource;
          }
          catch (DBException $e)
          {
            throw $e;
          }
          catch (DBConnectionException $e)
          {
            throw $e;
          }
      }

      /**
       *Returns true if database successfully completes removal of resource
       * with the resource id of $resourceId.  Else it returns false.
       */
      public static function deleteResource($resourceId)
      {
          try
          {
             $cnn = new DBConnection();
             $query = "DELETE FROM tResource WHERE Resource_id = '$resourceId'";
             $result = $cnn -> execute($query);
             $cnn->disconnect();
             return $result;
          }
          catch (DBException $e)
          {
             throw $e;
          }
          catch (DBConnectionException $e)
          {
            throw $e;
          }
      }

      /**
       * Returns array indexed by resourceName. Returns empty array if there
       * are no resources.
       */
      public static function findAllResources()
      {
          try
          {
              $cnn = new DBConnection();
              $resource = new Resource();
              $resourceArray = array();
              $query = "SELECT * FROM tResource";
              $result = $cnn -> execute($query);
              $num = mysql_numrows($result);
              $i = 0;
              while($i < $num)
              {
                  $resource = new Resource();
                  $resourceId = mysql_result($result, $i, "Resource_id");
                  $ownerId = mysql_result($result, $i, "Owner_id");
                  $resourceDescription = mysql_result($result, $i, "Description");
                  $resourceLocation = mysql_result($result, $i, "Location");
                  $resourceName = mysql_result($result, $i, "Name");
                  $resourceGroup = mysql_result($result, $i, "Group_Name");
                  $resourceStatus = mysql_result($result, $i, "Status_Value");
                  $resource->setAll($resourceName, $ownerId, $resourceDescription,
                                    $resourceLocation, $resourceGroup, $resourceStatus);
                  $resource -> setResourceId($resourceId);
                  $resourceArray[] = $resource;
                  $i++;
              }
              $cnn -> disconnect();
              return $resourceArray;
          }
          catch (DBException $e)
          {
              throw $e;
          }
          catch (DBConnectionException $e)
          {
              throw $e;
          }
      }
      /**
       * Updates a resource with new data.  Throws DBException if the update
       * is unsuccessful.
       */
       public static function updateResourceDetail($resource)
       {
           try
           {
               $cnn = new DBConnection();
               $resourceId  = $resource -> getResourceId();
               $ownerId  = $resource -> getOwnerId();
               $resourceName = $resource -> getResourceName();
               $resourceDescription = $resource -> getResourceDescription();
               $resourceLocation = $resource -> getResourceLocation();
               $resourceGroup = $resource -> getResourceGroup();
               $resourceStatus = $resource -> getResourceStatus();
               $query = "UPDATE tResource SET Name = '$resourceName', Owner_id = '$ownerId',
                                              Description = '$resourceDescription', Location = '$resourceLocation',
                                              Group_Name = '$resourceGroup', Status_Value = '$resourceStatus' WHERE Resource_id = '$resourceId'";
               
               $result = $cnn -> execute($query);
               if ($result == FALSE)
               {
                   throw new DBException("Error on Database Update Query.");
               }
               $cnn->disconnect();
           }
           catch (DBException $e)
           {
               throw $e;
           }
           catch (DBConnectionException $e)
           {
               throw $e;
           }
       }

        /**
         * Updates a resource with resource information in $resource.  Throws
         * DBException if insert is unsuccessful.
         */
       public static function insertResourceDetail($resource)
       {
           try
           {
               $cnn = new DBConnection();
               
               $ownerId = $resource -> getOwnerId();
               $resourceDescription = $resource -> getResourceDescription();
               $resourceLocation = $resource -> getResourceLocation();
               $resourceName = $resource -> getResourceName();
               $resourceGroup = $resource -> getResourceGroup();
               $resourceStatus = $resource -> getResourceStatus();
               
               $query = "INSERT INTO tResource (Owner_id, Description, Location, Name, Group_Name, Status_Value)
                                         VALUES ('$ownerId', '$resourceDescription', '$resourceLocation',
                                                '$resourceName', '$resourceGroup', '$resourceStatus')";

               $result = $cnn -> execute($query);
               $cnn -> disconnect();
           }
           catch (DBException $e)
           {
               throw $e;
           }
           catch (DBConnectionException $e)
           {
                throw $e;
           }
       }
}
?>